<?php

namespace App\Http\Requests\Admin\Customer\Banner;

use App\Http\Requests\ApiRequest;
use App\Models\Customer\BannerType;
use App\Services\Utils\Guard;
use Illuminate\Http\UploadedFile;
use Illuminate\Validation\Rule;

/**
 * @property int banner_type_id
 * @property string name
 * @property UploadedFile image
 * @property string data
 * @property string started_at
 * @property string ended_at
 * @property int priority
 * @property array values
 * @property string type
 */
class BannerStoreRequest extends ApiRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $customer_id = Guard::customerAdmin()->customer_id;

        return [
            'type'               => 'required|string|in:Store,DeviceAccount',
            'values'             => 'required|array',
            'values.*'           => 'required|int|min:1',
            'banner_type_id'     => 'required|int|min:1',
            'name'               => ['required', 'max:'.self::MAX_STRING, 'string', Rule::unique('banners', 'name')
                ->where('customer_id', $customer_id)
            ],
            'image'              => 'required|file|max:2048',
            // [#623] add nullable to prevent if data is empty string
            'data'               => 'required_if:banner_type_id,'.BannerType::ID_TO_PRODUCT.'|nullable|string',
            'priority'           => 'nullable|int|between:0,10000',
            'started_at'         => 'nullable|date',
            'ended_at'          => 'nullable|date',
        ];
    }
}
